package com.hung.triple.registry;

import com.hung.triple.bootstrap.config.TripleRegistryConfig;
import com.hung.triple.common.enums.TripleRegisterType;
import com.hung.triple.common.exception.TripleRpcException;
import com.hung.triple.registry.nacos.NacosServiceRegistry;
import com.hung.triple.registry.zookeeper.ZKRegister;

/**
 * @author Hung
 * @date 2023/1/11 1:26
 */
public class RegisterFactory {

    private static volatile Register register;

    public static Register getRegister() {
        if (register == null) {
            synchronized (RegisterFactory.class) {
                if (register == null) {
                    // 连接注册中心，写入 server 信息
                    if (TripleRegistryConfig.getRegisterType() == TripleRegisterType.ZOOKEEPER) {
                        register = new ZKRegister(TripleRegistryConfig.getAddressList());
                    } else if (TripleRegistryConfig.getRegisterType() == TripleRegisterType.NACOS) {
                        register = new NacosServiceRegistry(TripleRegistryConfig.getAddressList());
                    } else {
                        throw new TripleRpcException("RegisterType unsupported");
                    }
                }
            }
        }
        return register;

    }

}
